#ifndef _UTIL_H_
#define _UTIL_H_

#define MAXRECVSTRING 255       /* Longest string to receive */
#define CLOSE_MSG     "CLOSE"   /* Message to "close connection" (stop sending NACK messages)*/
#define MAXNUMHOSTS   10        /* Maximum number of hosts to be controlled in the ack*/
#define MAXNUMMSG     100       /* Maximum number of messages*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/errno.h>  /* errno, perror */
#include <sys/time.h>   /* setitimer */
#include <signal.h>     /* signal */
#include <time.h>
#include "threads.h"
#include "protocol.h"
#include "linked_list_msg.h"

int wait;
double prob_erro;
char broadcastIP[255];                /* IP broadcast address */
char IPAddress	[255];
char LastMessageinOrder[255];
char ackHostsList [10][255];
char ackControl [10][100];
Header last_header;
int isNack;
int isAck;
int isInOrder;
int isLogging;
unsigned short broadcastPort;
unsigned short int message_id;
MessageList received_messages;

int send_broadcast_msg(char* msg, int sendMsgLen);

int send_unicast_msg(char* msg, int sendMsgLen, char* id_dest);

int recv_msg();

int getBroadcastAddress(char *interface, char *szBroadcast);

int getIPAddress(char *interface, char *szIPAddr);

void construct_address(struct sockaddr_in* broadcastAddr, char* address, unsigned short port);

void send_ack(int msg_id, char* id_dest);

void send_nack();

void mysettimer(int milisegundos);

void timer_handler(int signum);

void mysethandler();

void LogFile(char* logInfo, ...);


#endif /* _UTIL_H_ */
